package org.jeecg.modules.gongqiang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.front.entity.vo.AuthenticationUserhome;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.fund.entity.JiStaticCount;
import org.jeecg.modules.fund.mapper.JiStaticCountMapper;
import org.jeecg.modules.gongqiang.entity.Userhome;
import org.jeecg.modules.gongqiang.entity.UserhomeAuthentication;
import org.jeecg.modules.gongqiang.entity.YcYingshengUserwallt;
import org.jeecg.modules.gongqiang.mapper.UserhomeAuthenticationMapper;
import org.jeecg.modules.gongqiang.mapper.UserhomeMapper;
import org.jeecg.modules.gongqiang.service.IUserhomeAuthenticationService;
import org.jeecg.modules.gongqiang.service.IYcYingshengUserwalltService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 实名认证
 * @Author: jeecg-boot
 * @Date:   2024-08-24
 * @Version: V1.0
 */
@Service
public class UserhomeAuthenticationServiceImpl extends ServiceImpl<UserhomeAuthenticationMapper, UserhomeAuthentication> implements IUserhomeAuthenticationService {
    @Autowired
    private IYcYingshengUserwalltService ycYingshengUserwalltService;

    @Resource
    private UserhomeMapper userhomeMapper;

    @Resource
    private UserhomeAuthenticationMapper userhomeAuthenticationMapper;

    @Resource
    private JiStaticCountMapper  jiStaticCountMapper;




    @Override
    public Result<String> authentication(@RequestBody AuthenticationUserhome authenticationUserhome,Integer userhomeId)
    {
        YcYingshengUserwallt wallt = ycYingshengUserwalltService.getByNameCertNo(authenticationUserhome.getRealName(),authenticationUserhome.getCertNo());
        if(oConvertUtils.isNotEmpty(wallt))
        {
            Userhome userhome=userhomeMapper.selectById(userhomeId);
            userhome.setIsAuthentication(1);
            userhome.setYingShengMerchantNo(wallt.getMerchantno());
            userhomeMapper.updateById(userhome);
            UserhomeAuthentication userhomeAuthentication=new UserhomeAuthentication();
            userhomeAuthentication.setStatus(1);
            userhomeAuthentication.setCertType(1);
            userhomeAuthentication.setUserhomeId(userhomeId);
            userhomeAuthentication.setPhone(authenticationUserhome.getPhone());
            userhomeAuthentication.setRealName(authenticationUserhome.getRealName());
            userhomeAuthentication.setCertNo(authenticationUserhome.getCertNo());
            userhomeAuthenticationMapper.insert(userhomeAuthentication);
        }
        else
        {
            JiStaticCount jiCount=jiStaticCountMapper.getByFeild("create_wallet");
            jiCount.setValue(jiCount.getValue()+1);
            jiStaticCountMapper.updateById(jiCount);
            Result<JSONObject> result = YinShengUtil.WalletRegister(authenticationUserhome.getRealName(),authenticationUserhome.getCertNo(),authenticationUserhome.getPhone());
            if(result.isSuccess())
            {
                //银盛个人钱包跨数据新增
                JSONObject yingshengResult=result.getResult();
                YcYingshengUserwallt ycYingshengUserwallt=new YcYingshengUserwallt();
                ycYingshengUserwallt.setName(authenticationUserhome.getRealName());
                ycYingshengUserwallt.setCertno(authenticationUserhome.getCertNo());
                ycYingshengUserwallt.setPhone(authenticationUserhome.getPhone());
                ycYingshengUserwallt.setCerttype(1);
                ycYingshengUserwallt.setType(2);
                ycYingshengUserwallt.setCreatetime(new Date());
                ycYingshengUserwallt.setMerchantno(yingshengResult.getString("merchantNo"));
                ycYingshengUserwalltService.save(ycYingshengUserwallt);
                //实名记录新增
                Userhome userhome=userhomeMapper.selectById(userhomeId);
                userhome.setIsAuthentication(1);
                userhome.setYingShengMerchantNo(yingshengResult.getString("merchantNo"));
                userhomeMapper.updateById(userhome);
                UserhomeAuthentication userhomeAuthentication=new UserhomeAuthentication();
                userhomeAuthentication.setStatus(1);
                userhomeAuthentication.setCertType(1);
                userhomeAuthentication.setUserhomeId(userhomeId);
                userhomeAuthentication.setPhone(authenticationUserhome.getPhone());
                userhomeAuthentication.setRealName(authenticationUserhome.getRealName());
                userhomeAuthentication.setCertNo(authenticationUserhome.getCertNo());
                userhomeAuthenticationMapper.insert(userhomeAuthentication);
            }
            else
            {
                if(result.getMessage().contains("已注册成功,请勿重复注册"))
                {
                    Result<JSONObject> sresult = YinShengUtil.UserInfoByCertNo(authenticationUserhome.getCertNo());
                    if(sresult.isSuccess())
                    {
                        //银盛个人钱包跨数据新增
                        JSONObject yingshengResult=sresult.getResult();
                        YcYingshengUserwallt ycYingshengUserwallt=new YcYingshengUserwallt();
                        ycYingshengUserwallt.setName(authenticationUserhome.getRealName());
                        ycYingshengUserwallt.setPhone(authenticationUserhome.getPhone());
                        ycYingshengUserwallt.setType(2);
                        ycYingshengUserwallt.setCertno(authenticationUserhome.getCertNo());
                        ycYingshengUserwallt.setCerttype(1);
                        ycYingshengUserwallt.setCreatetime(new Date());
                        ycYingshengUserwallt.setMerchantno(yingshengResult.getString("merchantNo"));
                        ycYingshengUserwalltService.save(ycYingshengUserwallt);
                        //实名记录新增
                        Userhome userhome=userhomeMapper.selectById(userhomeId);
                        userhome.setIsAuthentication(1);
                        userhome.setYingShengMerchantNo(yingshengResult.getString("merchantNo"));
                        userhomeMapper.updateById(userhome);
                        UserhomeAuthentication userhomeAuthentication=new UserhomeAuthentication();
                        userhomeAuthentication.setStatus(1);
                        userhomeAuthentication.setCertType(1);
                        userhomeAuthentication.setUserhomeId(userhomeId);
                        userhomeAuthentication.setPhone(authenticationUserhome.getPhone());
                        userhomeAuthentication.setRealName(authenticationUserhome.getRealName());
                        userhomeAuthentication.setCertNo(authenticationUserhome.getCertNo());
                        userhomeAuthenticationMapper.insert(userhomeAuthentication);
                    }
                    else
                    {
                        return Result.error(result.getMessage());
                    }
                }
                else
                {
                    return Result.error(result.getMessage());
                }

            }
        }
        return Result.OK("认证成功!");
    }

    @Override
    public   UserhomeAuthentication getByUserhomeId(Integer userhomeId)
    {
        return this.baseMapper.getByUserhomeId(userhomeId);
    }

    @Override
    public IPage<UserhomeAuthentication> getPageList(Page page, QueryWrapper<UserhomeAuthentication> queryWrapper)
    {
       return this.baseMapper.getPageList(page,queryWrapper);
    }

}
